//发货单登记/应收登记（零担）
//赵永平
interface IorderReceivableBreakbulkViewModel extends ng.IScope{
    /**
     * 承运单数据
     */
    carrierData:invoiceManagement;
    /**
     * 订单数据
     */
    orderData:invoiceManagementOrder;
    /**
     * 子单数据
     */
    orderChilder:Array<invoiceManagementOrderChild>;
    /**
     * 货物类别下拉
     */
    goodsTypeDropDown: Array<GoodsTypeListItemResponse>;
    /**
     * 货物下拉数据
    */
    goodsDropDown: Array<GoodsListItemResponse>;
    /**
     * 货物数量单位下拉
     */
    goodsQuantityUnitDropDown: ValueListItemResponse[];
    /**
     * 费用类型枚举下拉
     */
    feeTypeDropDown:ValueListItemResponse[];

    /**
     * 结算类型枚举下拉
     */
    settlementTypeDropDown:ValueListItemResponse[];

    /**
     * 结算单位下拉
     */
    settleDropDown:Array<SettleListItemResponse>;

    /**
     * 含税枚举下拉
     */
    priceTypeDropDown:ValueListItemResponse[];

    /**
     * 吨位范围下拉
     */
    tonRangeDropDown:Array<LinePriceListItemResponse>;

    /**
     *添加应收
     */
    addReceivable():void;
    /**
     * 删除应收
     */
    deleteReceivable(id:receivableListResponse):void;

    /**
     * 应收列表
     */
    receivableList:Array<receivableListResponse>;
    /**
     * 编辑总单数据添加应收
     */
    editOrderTotalData(data:invoiceManagementOrder):void;
    /**
     * 编辑子单数据添加应收
     */
    editOrderChilderData(data:invoiceManagementOrderChild):void;
    /**
     * 编辑应收数据添加应收
     */
    eidtReceivableData(data:receivableListResponse):void;

    /**
     * 添加编辑应收保存数据
     */
    receivableDataSave:receivableDataSave|any;

    /**
     * 获取货物(change)
     */
    changeGoodsTypeEvent():void;

    /**
     * 获取吨位范围(change)
     */
    changeGoodsEvent(data:invoiceManagementOrder):void;

    /**
     * 获取订单详情事件
     */
    getOrderDetailEvent(data:invoiceManagementOrder):void;
    /**
     * 返回
     */
    shikpBack():void;

    /**
     * 线路信息
     */
    lineInfo:lineInfoRece|any;
    /**
     * 编辑-添加应收按钮
     */
    costName:string;

    /**
     * 结算单位自动补全
     */
    onSettleAutoComplate:(value:string) => any;
    /**
     * 结算单位选中事件
     */
    onsettleSelected: (select: angucomplete<SettleListItemResponse>) => void;
}

interface lineInfoRece {
    shipProvince:string;
    shipCity:string;
    shipArea:string;
    shipdetail:string;

    deliverProvince:string;
    deliverCity:string;
    deliverArea:string;
    deliverdetail:string;

    viaAddressList:string[];

    shipTime:string;
    arriveTime:string;
    mileage:number;
}

class orderReceivableBreakbulkController{
    /**
     * 承运单
     */
    carrierId:string;
    /**
     * 客户单位id
     */
    clientId:string;
    orderId:string;

    constructor(private $scope:IorderReceivableBreakbulkViewModel,private orderService:IorderService,private linePriceService:linePriceService,private settleService:ISettleService,private goodsTypeService:goodsTypeService,private goodsService:IGoodsService,private valueService:IValueService,private $location:ng.ILocationService,private $ngBootbox:BootboxService,private $state:ng.ui.IStateService){
        this.init();
    }

    //初始化
    init():void{
        this.orderId = "";
        this.carrierId = this.$location.search().id;
        this.getCarrierData();
        this.loadValueDropDown();
        this.$scope.getOrderDetailEvent=(item)=>{
            this.getOrderDetailEvent(item)
        };

        this.$scope.receivableDataSave = {};
        this.$scope.receivableDataSave.orderId = "";
        this.$scope.lineInfo = {};

        this.$scope.costName = "添加";

        this.$scope.shikpBack = this.shikpBack;

        this.orderService.changeIsCheckReceivable(this.carrierId,true);

        this.$scope.onSettleAutoComplate = this.onSettleAutoComplate;
        this.$scope.onsettleSelected = this.onsettleSelected;  
    }

    /**
     * 获取承运单数据
     */
    getCarrierData=()=>{
        this.orderService.invoiceManagementDetail(this.carrierId).then(res=>{
            this.$scope.carrierData = res.data;
            
        });
    };

    /**
     * 获取订单详情事件
     */
    getOrderDetailEvent=(data:invoiceManagementOrder)=>{
        this.$scope.orderData = data;
        this.$scope.orderChilder = data.childList;
        

        this.clientId = data.clientId;
        this.orderId = data.id;
        this.getReceivableList();

        this.$scope.receivableDataSave = {
            id:"",
            orderChildId:"",
            orderId:"",
            goodsId:"",
            goodsName:"",
            goodsTypeId:"",
            goodsTypeName:"",
            quantityOfGoods:"",
            goodsUnit:"",
            quantityOfGoodsTwo:"",
            goodsUnitTwo:"",
            tonnageRange:"",
            receivableSummary:"",
            includeTax:"",
            settleId:"",
            settle:"",
            settlementType:"",
            feeType:"",
            applyReceivable:""
        }
        this.$scope.lineInfo = {
            shipProvince:"",
            shipCity:"",
            shipArea:"",
            shipdetail:"",
            deliverProvince:"",
            deliverCity:"",
            deliverArea:"",
            deliverdetail:"",
            viaAddressList:[],
            shipTime:"",
            arriveTime:"",
            mileage:""
        }
        this.$scope.editOrderTotalData = this.editOrderTotalData;
        this.$scope.editOrderChilderData = this.editOrderChilderData;
        this.$scope.eidtReceivableData = this.eidtReceivableData;
        this.$scope.deleteReceivable = this.deleteReceivable;
        //下拉事件
        this.$scope.changeGoodsTypeEvent = this.changeGoodsTypeEvent;

        this.$scope.receivableDataSave.orderId = data.id;
    }

    /**
     * 获取应收列表
     */
    getReceivableList=()=>{
        this.orderService.receivableList(this.orderId).then(res=>{
            this.$scope.receivableList = res.data.data;
        });
    };


    /**
     * 编辑总单添加应收
     */
    editOrderTotalData = (data:invoiceManagementOrder) => {
        this.$scope.costName = "添加";
        let scRece = this.$scope.receivableDataSave;
        let scLine = this.$scope.lineInfo;

        
        scLine.shipProvince = data.originProvinceStr;
        scLine.shipCity = data.originCityStr;
        scLine.shipArea = data.originCountyStr;
        scLine.shipdetail = data.originDetails;

        scLine.deliverProvince = data.destinationProvinceStr;
        scLine.deliverCity = data.destinationCityStr;
        scLine.deliverArea = data.destinationCountyStr;
        scLine.deliverdetail = data.destinationDetails;
        
        scLine.viaAddressList = data.viaAddressList;
        scLine.mileage = data.mileage;
        scLine.shipTime = data.deliveryTime;
        scLine.arriveTime = data.arrivalTime;

        if(data.viaList){
            scLine.viaAddressList = data.viaList.split(",");
        }
        
        scLine.mileage = data.mileage;
        scLine.shipTime = data.deliveryTime;
        scLine.arriveTime = data.arrivalTime;

        scRece.orderChildId = "";
        scRece.orderId = data.id;
        scRece.goodsId = data.goodsId;
        scRece.goodsName = data.goodsName;
        scRece.goodsTypeId = data.goodsTypeId;
        scRece.goodsTypeName = data.goodsTypeName;
        scRece.quantityOfGoods = data.realQuantityOfGoods;
        scRece.goodsUnit = data.goodsUnitEnum;
        scRece.quantityOfGoodsTwo = data.quantityOfGoodsTwo;
        scRece.goodsUnitTwo = data.goodsUnitTwoEnum;
        scRece.tonnageRange = data.tonnageRange;
        scRece.receivableSummary = "";
        scRece.includeTax = "";
        scRece.settleId = "";
        scRece.settle = "";
        $("#reckoner_value").val("");
        scRece.settlementType = "";
        scRece.feeType = "";
        scRece.applyReceivable = null;

        this.clientId = data.clientId;
        
        //获取数据
        this.getGoodsTypeDropDown();
        this.getGoodsDropDown();
        this.getTonnageRange(data);
        this.getSettleDropDown();
        //事件
        this.$scope.changeGoodsEvent = ()=>{
            this.changeGoodsEvent(data)
        };
        this.$scope.addReceivable = this.addReceivable;
        
    }

    /**
     * 编辑子单添加应收
     */
    editOrderChilderData = (data:invoiceManagementOrderChild) => {
        this.$scope.costName = "添加";
        let scRece = this.$scope.receivableDataSave;
        let scLine = this.$scope.lineInfo;

        scLine.shipProvince = data.shipProvinceStr;
        scLine.shipCity = data.shipCityStr;
        scLine.shipArea = data.shipAreaStr;
        scLine.shipdetail = data.shipAddress;
        scLine.deliverProvince = data.deliverProvinceStr;
        scLine.deliverCity = data.deliverCityStr;
        scLine.deliverArea = data.deliverAreaStr;
        scLine.deliverdetail = data.deliverAddress;
        scLine.viaAddressList = data.viaAddressList;
        scLine.mileage = data.mileage;
        scLine.shipTime = data.shipTime;
        scLine.arriveTime = data.arriveTime;

        scRece.orderChildId = data.id;
        // scRece.orderId = "";订单编号在获取承运单获取
        scRece.goodsId = data.goodsId;
        scRece.goodsName = data.goodsName;
        scRece.goodsTypeId = data.goodsTypeId;
        scRece.goodsTypeName = data.goodsType;
        scRece.quantityOfGoods = data.goodsNum;
        scRece.goodsUnit = data.goodsNumUnitEnum;
        scRece.quantityOfGoodsTwo = data.goodsNumTwo;
        scRece.goodsUnitTwo = data.goodsNumUnitTwoEnum;
        scRece.tonnageRange = data.tonRange;
        scRece.receivableSummary = data.receivableRemarks;
        scRece.includeTax = data.includeTaxEnum;
        scRece.settleId = data.settleId;
        scRece.settle = data.reckoner;
        $("#reckoner_value").val(data.reckoner);

        scRece.settlementType = data.settleTypeEnum;
        scRece.feeType = "";
        scRece.applyReceivable = null;

        // this.clientId = "";在获取承运单数据时获取
        //获取数据
        this.getGoodsTypeDropDown();
        this.getGoodsDropDown();
        this.getTonnageRange(data);
        // this.getSettleDropDown();
        //事件
        this.$scope.changeGoodsEvent = ()=>{
            this.changeGoodsEvent(data);
        };
        this.$scope.addReceivable = this.addReceivable;
    }

    /**
     * 编辑应收
     */
    eidtReceivableData = (data:receivableListResponse) => {
        this.$scope.costName = "保存";

        let scRece = this.$scope.receivableDataSave;
        let scLine = this.$scope.lineInfo;

        scLine.shipProvince = data.originProvince;
        scLine.shipCity = data.originCity;
        scLine.shipArea = data.originCounty;
        scLine.shipdetail = data.origin.details;

        scLine.deliverProvince = data.destinationProvince;
        scLine.deliverCity = data.destinationCity;
        scLine.deliverArea = data.destinationCounty;
        scLine.deliverdetail = data.destination.details;

        if(data.viaList){
            scLine.viaAddressList = data.viaList.split(",");
        }
        
        scLine.mileage = data.mileage;
        scLine.shipTime = data.deliveryTime;
        scLine.arriveTime = data.arrivalTime;


        scRece.orderChildId = data.orderChildId;
        scRece.orderId = data.orderId;
        scRece.goodsId = data.goodsId;
        scRece.goodsName = data.goodsName;
        scRece.goodsTypeId = data.goodsTypeId;
        scRece.goodsTypeName = data.goodsName;
        scRece.quantityOfGoods = data.goodsCount;
        scRece.goodsUnit = data.goodsUnit;
        scRece.quantityOfGoodsTwo = data.goodsCountTwo;
        scRece.goodsUnitTwo = data.goodsUnitTwo;
        scRece.tonnageRange = data.tonnageRange;
        scRece.receivableSummary = data.receivableRemark;
        scRece.includeTax = String(data.includeTax);
        scRece.settleId = data.settleId;
        scRece.settle = data.settle;
        $("#reckoner_value").val(data.settle);
        scRece.settle = data.settle;
        scRece.settlementType = String(data.settlementType);
        scRece.feeType = String(data.feeType);
        scRece.applyReceivable = data.applyReceivable;
        //获取数据
        this.getGoodsTypeDropDown();
        this.getGoodsDropDown();
        // this.getSettleDropDown();
        this.getTonnageRange(data);
        //事件
        this.$scope.changeGoodsEvent = ()=>{
            this.changeGoodsEvent(data)
        };

        this.$scope.addReceivable = ()=>{
            this.editReceivable(data.receivableId);
        }
      
    }

    /**
     * 改变货物类别获取货物事件
     */
    changeGoodsTypeEvent=()=>{
        this.goodsService.getList(this.clientId,"",this.$scope.receivableDataSave.goodsTypeId,0,-1).then(res=>{
            this.$scope.goodsDropDown = res.data.data;
        });        
    };

    /**
     * 改变货物货物吨位范围事件(问题)
     */
    changeGoodsEvent=(data:invoiceManagementOrder|invoiceManagementOrderChild|receivableListResponse)=>{
        this.changeGoodgetTonnageRange(data);
    };

    /**
     * 获取货物类别下拉
     */
    getGoodsTypeDropDown=()=>{
        this.goodsTypeService.getList(this.clientId,"",0,-1).then(res=>{
            this.$scope.goodsTypeDropDown = res.data.data;
        });
    };
    /**
     * 返回
     */
    shikpBack():void{
        // window.history.back();
         this.$state.go("app.order.orderRegisterList",{name:"orderRegisterList"});
    }

    /**
     * 获取货物下拉
     */
    getGoodsDropDown=()=>{
        this.goodsService.getList(this.clientId,"",this.$scope.receivableDataSave.goodsTypeId,0,-1).then(res => {
            this.$scope.goodsDropDown = res.data.data;
        });
    };

    /**
     * 获取结算单位下拉
     */
    getSettleDropDown():void{
        this.settleService.getList(this.clientId,"","",true,0,-1).then(res => {
            this.$scope.settleDropDown = res.data.data;      
        });
    };

    /**
     * 改变货物货物吨位范围事件
     */
    changeGoodgetTonnageRange(data:invoiceManagementOrder|invoiceManagementOrderChild|receivableListResponse):void{
        if(this.isOrderTotalTypeResponse(data)){
            this.linePriceService.getList(this.clientId,this.$scope.receivableDataSave.goodsTypeId,"1",data.originProvince,data.originCounty,data.originCounty,data.destinationProvince,data.destinationCity,data.destinationCounty,"",this.$scope.receivableDataSave.goodsId,data.originDetails,data.destinationDetails,0,-1).then(res=>{
                this.$scope.tonRangeDropDown = res.data.data;
            });
        }else if(this.isOrderchildTypeResponse(data)){
            this.linePriceService.getList(this.clientId,this.$scope.receivableDataSave.goodsTypeId,"1",data.shipProvince,data.shipCity,data.shipArea,data.deliverProvince,data.deliveryCity,data.deliverArea,"",this.$scope.receivableDataSave.goodsId,data.shipAddress,data.deliverAddress,0,-1).then(res=>{
                this.$scope.tonRangeDropDown = res.data.data;
            });
        }else if(this.isOrderReceivableTypeResponse(data)){
            this.linePriceService.getList(this.clientId,this.$scope.receivableDataSave.goodsTypeId,"1",data.origin.province,data.origin.city,data.origin.county,data.destination.province,data.destination.city,data.destination.county,"",this.$scope.receivableDataSave.goodsId,data.origin.details,data.destination.details,0,-1).then(res=>{
                this.$scope.tonRangeDropDown = res.data.data;
            });
        }
    };

    /**
     * 获取吨位范围
     */
    getTonnageRange(data:invoiceManagementOrder|invoiceManagementOrderChild|receivableListResponse):void{
        if(this.isOrderTotalTypeResponse(data)){
            this.linePriceService.getList(this.clientId,data.goodsTypeId,"1",data.originProvince,data.originCounty,data.originCounty,data.destinationProvince,data.destinationCity,data.destinationCounty,"",data.goodsId,data.originDetails,data.destinationDetails,0,-1).then(res=>{
                this.$scope.tonRangeDropDown = res.data.data;
            });
        }else if(this.isOrderchildTypeResponse(data)){
            this.linePriceService.getList(this.clientId,data.goodsTypeId,"1",data.shipProvince,data.shipCity,data.shipArea,data.deliverProvince,data.deliveryCity,data.deliverArea,"",data.goodsId,data.shipAddress,data.deliverAddress,0,-1).then(res=>{
                this.$scope.tonRangeDropDown = res.data.data;
            });
        }else if(this.isOrderReceivableTypeResponse(data)){
            this.linePriceService.getList(this.clientId,data.goodsTypeId,"1",data.origin.province,data.origin.city,data.origin.county,data.destination.province,data.destination.city,data.destination.county,"",data.goodsId,data.origin.details,data.destination.details,0,-1).then(res=>{
                this.$scope.tonRangeDropDown = res.data.data;
            });
        }

    }

    /**
     * 添加应收
     */
    addReceivable = () => {
        var settleName = $("#reckoner_value").val();
        if(settleName=="" || this.$scope.receivableDataSave.settleId ==""){
            this.$ngBootbox.alert("请填写结算单位");
            return;
        }
        if (!$("#receivableForm").valid()) { return; };
        this.$ngBootbox.confirm("确定添加该数据吗？").then(()=>{
            if(!this.$scope.receivableDataSave.goodsUnitTwo || this.$scope.receivableDataSave.goodsUnitTwo==""){this.$scope.receivableDataSave.goodsUnitTwo="0";}
            this.orderService.addReceivable(
                this.$scope.receivableDataSave.orderChildId,
                this.$scope.receivableDataSave.orderId,
                this.$scope.receivableDataSave.goodsId,
                // this.$scope.receivableDataSave.goodsName,
                $("#goodsId option:selected").text(),
                this.$scope.receivableDataSave.goodsTypeId,
                $("#goodsType option:selected").text(),
                // this.$scope.receivableDataSave.goodsTypeName,
                this.$scope.receivableDataSave.quantityOfGoods,
                this.$scope.receivableDataSave.goodsUnit,
                this.$scope.receivableDataSave.quantityOfGoodsTwo,
                this.$scope.receivableDataSave.goodsUnitTwo,
                // $("#tonRange option:selected").text(),
                this.$scope.receivableDataSave.tonnageRange,
                this.$scope.receivableDataSave.receivableSummary,
                this.$scope.receivableDataSave.includeTax,
                this.$scope.receivableDataSave.settleId,
                $("#reckoner_value").val(),
                // this.$scope.receivableDataSave.settle,
                this.$scope.receivableDataSave.settlementType,
                this.$scope.receivableDataSave.feeType,
                this.$scope.receivableDataSave.applyReceivable
            ).then(res=>{
                 this.getReceivableList();
            });
        },()=>{return;});
    };




    /**
     * 编辑应收
     */
    editReceivable = (id:string) => {
        if (!$("#receivableForm").valid()) { return; };
        this.$ngBootbox.confirm("确定保存该数据吗？").then(()=>{
            if(!this.$scope.receivableDataSave.goodsUnitTwo || this.$scope.receivableDataSave.goodsUnitTwo==""){this.$scope.receivableDataSave.goodsUnitTwo="0";}
            this.orderService.editReceivable(
                id,
                this.$scope.receivableDataSave.orderChildId,
                this.$scope.receivableDataSave.orderId,
                this.$scope.receivableDataSave.goodsId,
                // this.$scope.receivableDataSave.goodsName,
                 $("#goodsId option:selected").text(),
                this.$scope.receivableDataSave.goodsTypeId,
                // this.$scope.receivableDataSave.goodsTypeName,
                $("#goodsType option:selected").text(),
                this.$scope.receivableDataSave.quantityOfGoods,
                this.$scope.receivableDataSave.goodsUnit,
                this.$scope.receivableDataSave.quantityOfGoodsTwo,
                this.$scope.receivableDataSave.goodsUnitTwo,
                this.$scope.receivableDataSave.tonnageRange,
                // $("#tonRange option:selected").text(),
                this.$scope.receivableDataSave.receivableSummary,
                this.$scope.receivableDataSave.includeTax,
                this.$scope.receivableDataSave.settleId,
                // this.$scope.receivableDataSave.settle,
                $("#reckoner_value").val(),
                this.$scope.receivableDataSave.settlementType,
                this.$scope.receivableDataSave.feeType,
                this.$scope.receivableDataSave.applyReceivable
            ).then(res=>{
                 this.getReceivableList();
            });
        },()=>{return;});
    };


    /**
     * 删除应收
     */
    deleteReceivable = (data:receivableListResponse) => {
        //不能删除已创建的应收信息在页面中控制
        this.$ngBootbox.confirm("确定删除该数据吗?").then(()=>{
            if(data.orderChildId){
                this.orderService.delReceivableByChildId(data.orderId,data.receivableId,data.orderChildId).then(res=>{
                    this.getReceivableList();
                })
            }else{
                this.orderService.delReceivableByOrderId(data.orderId,data.receivableId).then(res=>{
                    this.getReceivableList();
                })
            }
        },()=>{return});
    };

    //断言
    isOrderTotalTypeResponse(response:invoiceManagementOrder|invoiceManagementOrderChild|receivableListResponse):response is invoiceManagementOrder {
        return (<invoiceManagementOrder>response).originProvince !== undefined;
    }
    isOrderchildTypeResponse(response:invoiceManagementOrder|invoiceManagementOrderChild|receivableListResponse):response is invoiceManagementOrderChild {
        return (<invoiceManagementOrderChild>response).shipProvince !== undefined;
    }
    isOrderReceivableTypeResponse(response:invoiceManagementOrder|invoiceManagementOrderChild|receivableListResponse):response is receivableListResponse {
        return (<receivableListResponse>response).origin !== undefined;
    }

    /**
     * 枚举下拉
     */
    loadValueDropDown():void{
        this.$scope.goodsQuantityUnitDropDown = this.valueService.getGoodQuantityUnitList().data;
        this.$scope.feeTypeDropDown = this.valueService.getReceivableFeeTypeList().data;
        this.$scope.settlementTypeDropDown = this.valueService.getSettlementTypeList().data;
        this.$scope.priceTypeDropDown = this.valueService.getLinePriceTypeList().data;
    };



     /**
     * 结算单位选中事件
     */
    onsettleSelected=(selected:angucomplete<SettleListItemResponse>)=>{
        if(selected){
            this.$scope.receivableDataSave.settleId = selected.description.id;
            this.$scope.receivableDataSave.settle = selected.description.name;
            $("#reckoner_value").val(selected.description.name)

        }else{
            this.$scope.receivableDataSave.settleId = "";
        }
    }

    /**
     * 结算单位
     */
    onSettleAutoComplate = (value: string) => {
        if (!this.clientId) { return; };
        return this.settleService.getList(this.clientId, value, "", true, 0, 5).then(result => {
            return result.data.data;
        });
    }
}
angular.module("tms").controller("orderReceivableBreakbulkController",["$scope","orderService","linePriceService","settleService","goodsTypeService","goodsService","valueService","$location","$ngBootbox",orderReceivableBreakbulkController])
